﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using ZHDJ.Core;
using ZHDJ.Data.Models.Center;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.PartyRepresentative
{
    public partial class MyPartyRepresentativeSuggestionEdit : PageBase
    {
        #region 属性
       

        /// <summary>
        /// 工作室ID
        /// </summary>
        public string StudioID
        {
            get
            {
                return GetQueryIntValue("StudioID"); ;
            }
        }
        #endregion
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get { return ""; }
        }

        #endregion

        #region 页面属性

        public string EditorContent { get; set; }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            //工作室加载
            ControlBusiness.ControlBind(
                ddlStudio, DB.PartyRepresentativeStudios.OrderBy(a => a.CreateTime),
                "StudioName", "ID");
            ddlStudio.SelectedValue = StudioID;
            //党代表加载 
            ControlBusiness.ControlBind(ddlRepresentativeID, DB.PartyRepresentativeInfos.OrderBy(a => a.OrderNo), "UserName", "ID");
            ControlBusiness.ControlBindDataDictionary(ddlSuggestionCategory, SysDataDictionary.PartyRepresentativeSuggestionCategory, true);
            ControlBusiness.ControlBindDataDictionary(ddlHandlingType, SysDataDictionary.PartyRepresentativeHandlingType, true);

            if (IsSystemAdmin)
            {
                this.ddlStudio.Readonly = false;
            }
            else
            {
                this.ddlStudio.Readonly = true;
            }
        }


        private void LoadData()
        {
            if (string.IsNullOrEmpty(StudioID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                var current = DB.PartyRepresentativeSuggestions.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtTitle.Text = current.Title;
                txtUserName.Text = ControlBusiness.GetMembersPickerText(current.SuggestionBy);
                txtMobileNo.Text = current.MobileNo;
                txtPost.Text = current.Post;
                ddlRepresentativeID.SelectedValue = current.RepresentativeID;
                txtWorkUnit.Text = current.WorkUnit;
                txtContent.Text = current.Content;
                this.ddlStatus.SelectedValue = current.Status.ToString();
                this.txtReply.Text = current.Reply;
                this.ddlHandlingType.SelectedValue = current.HandlingType;
                txtAge.Text = current.Age;
                ddlSex.SelectedValue = current.Sex;
                ddlIsOpen.SelectedValue = current.IsOpen ? "1" : "0";
                txtAddress.Text = current.Address;
                txtPoliticsLineament.Text = current.PoliticsLineament;
                ddlSuggestionCategory.SelectedValue = current.SuggestionCategory;
                txtZip.Text = current.Zip;
                if (current.Status != 1)
                {
                    this.txtReply.Text = current.Reply;
                    this.ddlHandlingType.SelectedValue = current.HandlingType;
                    this.ddlStatus.SelectedValue = current.Status.ToString();
                }
                
                    this.ddlStatus.Readonly = true;
                    this.ddlHandlingType.Readonly = true;
                    
                    this.txtReply.Readonly = true;
                
            }

        }

        #endregion

        #region Events

        private void SaveData()
        {
            if (string.IsNullOrEmpty(StudioID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }

            string id = GetQueryIntValue("id");
            if (string.IsNullOrEmpty(id))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }

            string imageUrl = string.Empty;
            PartyRepresentativeSuggestion editModel;

            //更新
            editModel = DB.PartyRepresentativeSuggestions.Find(id);
            if (editModel == null)
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            editModel.ModifyTime = DateTime.Now;
            editModel.ModifyBy = CurrentUser.LoginID;
            editModel.Status = Convert.ToInt32(this.ddlStatus.SelectedValue);
            editModel.HandlingType = ddlHandlingType.SelectedValue;
            editModel.Status = Convert.ToInt32(ddlStatus.SelectedValue);
            editModel.Reply = txtReply.Text;
            if (editModel.Status == 2)
            {
                editModel.HandlingTime = DateTime.Now;
            }
            if (editModel.Status == 3)
            {
                editModel.FinishTime = DateTime.Now;
            }

            if (DB.SaveChanges() > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();


        }


        #endregion

    }
}